package com.mobile.framework.util;

import java.util.ArrayList;
import java.util.Collection;

/**
 * This class is a extension implementation of java.util.ArrayList. It will be
 * used to contain paging data and provide three new attributes: pageCount -
 * total page count of result set totalRowCount - total row count of result set
 * currentPage - current page number
 */
public class PageList extends ArrayList {
	
	//页的总数.
	protected int pageCount = 0;

	//当前页 从1开始.
	protected int currentPage = 0;
	
	//总条数
	protected int totalRowCount = 0;

	/**
	 * Constructs an empty list with an initial capacity of ten. The pageCount,
	 * currentPage and totalRowCount are initialized to zero.
	 */
	public PageList() {
		super();
	}

	/**
	 * Constructs a list containing the elements of the specified collection, in
	 * the order they are returned by the collection's iterator. The pageCount,
	 * currentPage and totalRowCount are initialized to zero.
	 */
	public PageList(Collection c) {
		super(c);
	}

	/**
	 * Constructs an empty list with the specified initial capacity.The
	 * pageCount, currentPage and totalRowCount are initialized to zero.
	 */
	public PageList(int initialCapacity) {
		super(initialCapacity);
	}

	/**
	 * Set the total page count
	 */
	public void setPageCount(int pageCount) {
		if (pageCount < 0) {
			pageCount = 0;
		}
		this.pageCount = pageCount;
	}

	/**
	 * Set the current page number
	 */
	public void setCurrentPage(int currentPage) {
		if (currentPage < 0) {
			currentPage = 0;
		}
		this.currentPage = currentPage;
	}

	/**
	 * Set the TOTAL row count. It's NOT the row count of current page, which
	 * can be achieved through method size().
	 */
	public void setTotalRowCount(int totalRowCount) {
		this.totalRowCount = totalRowCount;
	}

	/**
	 * Get the total page count
	 */
	public int getPageCount() {
		return pageCount;
	}

	/**
	 * Get the current page count
	 */
	public int getCurrentPage() {
		return currentPage;
	}

	/**
	 * Get the total row count
	 */
	public int getTotalRowCount() {
		return totalRowCount;
	}

	/**
	 * 根据记录总数得到页数
	 * 
	 */
	public void calcPageCount(int pageSize) {
		if (pageSize < 1) {
			pageSize = 1;
		}
		int n = (totalRowCount % pageSize);
		if (n == 0) {
			setPageCount((totalRowCount / pageSize));
		} else {
			setPageCount(((totalRowCount - n) / pageSize + 1));
		}
	}
}